/// <reference path="../../lib/gd3d.d.ts" />

//需加上这个反射标记，场景才能通过名字找到这个类，并自动创建他
@gd3d.reflect.userCode
class SkinnedMeshTest implements gd3d.framework.IUserCode
{
    app: gd3d.framework.application;
    scene: gd3d.framework.scene;

    private camera: gd3d.framework.camera;
    private elong: gd3d.framework.transform;

    private cube:gd3d.framework.transform;
    
    onStart(app: gd3d.framework.application) {
        let that = this;
        this.app = app;

        this.scene = this.app.getScene();

        let loaded1, loaded2;
        let testStatic = false;

        // 加载shader
        let shaderAssetUrl = "resources/shader/shader.assetbundle.json";
        this.app.getAssetMgr().loadCompressBundle(shaderAssetUrl, (state) => {
            if (state.isfinish) {
                // 加载并显示模型
                let elongAssetUrl = "resources/prefabs/" + "elong/elong.assetbundle.json";
                this.app.getAssetMgr().load(elongAssetUrl, gd3d.framework.AssetTypeEnum.Auto, (s) => {
                    if (s.isfinish) {
                        createObjects();
                    }
                });
            }
        });

        function createObjects() {
            let _prefab1: gd3d.framework.prefab = that.app.getAssetMgr().getAssetByName("elong.prefab.json") as gd3d.framework.prefab;        
            let count = 0;
            console.log(_prefab1);
            let children:gd3d.framework.Transform[] = _prefab1["trans"].children.slice();
            _prefab1["trans"].children = [children[0]];

            let container = _prefab1["trans"] as gd3d.framework.Transform;
            let mesh:gd3d.framework.Mesh;

            let renderers = [];
            for(let i = 1; i < children.length; i++) {
                renderers.push(children[i].gameObject.renderer);
            }
            console.log(renderers[1])
            let renderer = gd3d.framework.SkinnedMeshRenderer.combine(renderers);
            console.log(renderer);
            children[1].gameObject.removeAllComponents();
            children[1].gameObject["componentsInit"] = [];
            children[1].gameObject.addComponentDirect(renderer);
            // renderer.gameObject = children[1].gameObject;
            // children[1].gameObject.renderer = renderer;
            // children[1].gameObject["components"][0].comp = renderer;
            
            _prefab1["trans"].children.push(children[1]);


            for (let i = -10; i <=10; i++)
            {
                for (let j = -10; j <=10; j++)
                {
                    count++;
                    let _prefab = _prefab1;
                    let trans = _prefab.getCloneTrans();
                    if(i === 0 && j === 0)
                    console.log(trans);
                    // let trans = _prefab["trans"];

                    trans.children[1].gameObject.renderer["bonesList"] = renderer.bonesList;

                
                    that.scene.addChild(trans);
                    trans.localTranslate = new gd3d.math.Vector3(i * 5, 0, j * 5);
                    trans.markDirty();

                    var player = trans.gameObject.getComponent(gd3d.framework.StringUtil.COMPONENT_ANIPLAYER) as gd3d.framework.AniPlayer;
                    player.clips[0].loop = true;
                }
            }
            objCam.markDirty();
        }

        //添加一个摄像机
        var objCam = new gd3d.framework.Transform();
        objCam.name = "sth.";
        this.scene.addChild(objCam);
        this.camera = objCam.gameObject.addComponent("Camera") as gd3d.framework.Camera;
        this.camera.near = 0.01;
        this.camera.far = 1000;
        this.camera.fov = 45;
        objCam.localTranslate = new gd3d.math.Vector3(0, 200, 0);
        objCam.lookatPoint(new gd3d.math.Vector3(0, 0, 0));
        objCam.markDirty();//标记为需要刷新
    }

    private timer: number = 0;
    onUpdate(delta: number) {
        this.timer += delta;
        
    }

    isClosed(): boolean {
        return false;
    }
}
